package com.online.pay.common.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.dozer.DozerBeanMapper;

/**
 * 简单封装Dozer, 实现深度转换Bean<->Bean的Mapper.实现:
 * 1. 持有Mapper的单例.
 * 2. 返回值类型转换.
 * 3. 批量转换Collection中的所有对象.
 * 4. 区分创建新的B对象与将对象A值复制到已存在的B对象两种函数.
 * @author Qiurz
 *
 */
public class BeanMapper {
	
	private static DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
	
	/**
	 * 基于DozerBeanMapper转换对象
	 * @param sourceObj
	 * @param targetObj
	 * @return
	 */
	public static <T> T map(Object sourceObj,Class<T> targetObj) {
		return dozerBeanMapper.map(sourceObj, targetObj);
	}
	
	/**
	 * 基于Dozer转换Collection中对象的类型.
	 * @param sourceList
	 * @param targetClass
	 * @return
	 */
	public static <T> List<T> mapList(Collection<T> sourceList,Class<T> targetClass) {
		if (sourceList == null || sourceList.isEmpty()) {
			return null;
		}
		List<T> targetList = new ArrayList<>();
		for (Object obj : sourceList) {
			T targetObj = dozerBeanMapper.map(obj, targetClass);
			targetList.add(targetObj);
		}
		return targetList;
	}

	/**
	 * 基于Dozer将对象sourceObj中的值拷贝到targetObj中
	 * @param sourceObj 原对象
	 * @param targetObj 目标对象
	 */
	public static void copy(Object sourceObj, Object targetObj) {
		dozerBeanMapper.map(sourceObj, targetObj);
	}

}
